using Electric.Application.AppService.Base;
using Electric.Application.Contracts.AppService.Todo;
using Electric.Application.Contracts.Dto.Identity.Todos;
using Electric.Application.Contracts.Dto.Todos;
using Electric.Core.Exceptions;
using Electric.Domain.Entitys.Todos;
using Electric.Domain.Manager.Todo;
using Microsoft.AspNetCore.Builder;

namespace Electric.Application.AppService.Todo;

public class TodoAppService : BaseAppService, ITodoAppService
{
    private readonly TodoManager _todoManager;

    public TodoAppService(TodoManager todoManager)
    {
        _todoManager = todoManager;
    }

    public async Task DeleteAsync(Guid id)
    {
        //判断用户Id，是否存在
        // var user = await _userManager.FindByIdAsync(id.ToString());
        var todo = await _todoManager.FindByIdAsync(id);
        if (todo == null)
        {
            return;
        }
        //删除todo
        await _todoManager.DeleteAsync(id);
        //提交所有更新
        await _unitOfWork.SaveChangesAsync();
    }

    public async Task<TodoDto> GetAsync(Guid id)
    {
        EleTodo eleTodo = await _todoManager.FindByIdAsync(id);
        TodoDto todoDto = _mapper.Map<TodoDto>(eleTodo);
        return todoDto;
    }

    public async Task<TodoPageResponseDto> GetPagedListAsync(TodoPageRequestDto userPageRequestDto)
    {
        List<EleTodo> eleTodos = await _todoManager.GetListAsync(
            userPageRequestDto.Name,
            userPageRequestDto.Remark,
            userPageRequestDto.Status,
            userPageRequestDto.Page,
            userPageRequestDto.PrePage
        );

        long total = await _todoManager.GetCountAsync(userPageRequestDto.Name, userPageRequestDto.Remark, userPageRequestDto.Status);

        try
        {
            var todoDtos = _mapper.Map<List<TodoDto>>(eleTodos);
            return new TodoPageResponseDto()
            {
                Page = userPageRequestDto.Page,
                PrePage = userPageRequestDto.PrePage,
                Todos = todoDtos,
                Total = total
            };
        }
        catch (System.Exception ex)
        {
            System.Console.WriteLine(ex);
            throw;
        }
        //返回
    }

    public async Task<EleTodo> InsertAsync(TodoCreateDto todoCreateDto)
    {
        EleTodo eleTodoCreate = _mapper.Map<EleTodo>(todoCreateDto);
        if (_eleSession == null)
        {
            System.Console.WriteLine("session is null");
        }
        eleTodoCreate.SetUserId(_eleSession!.UserId);
        EleTodo eleTodo = await _todoManager.CreateAsync(eleTodoCreate);
        //提交所有更新
        return eleTodo;
    }

    public async Task<TodoDto> UpdateAsync(Guid id, TodoUpdateDto todoUpdateDto)
    {
        //判断用户Id，是否存在
        var entity = await _todoManager.FindByIdAsync(id);

        if (entity == null)
        {
            throw new BusinessException("该todo Id不存在，Id:" + id.ToString());
        }
        //更新用户信息
        entity.Name = todoUpdateDto.Name;
        entity.Status = todoUpdateDto.Status;
        entity.Remark = todoUpdateDto.Remark;

        // 更新todo
        EleTodo eleTodo = await _todoManager.UpdateAsync(entity);

        // //更新用户失败
        if (eleTodo == null)
        {
            // ThrowBusinessException(eleTodo, "更新用户失败！");
        }

        return _mapper.Map<TodoDto>(entity);
    }
}

